package com.example.hospital.controller;

import com.example.hospital.common.ApiResponse;
import com.example.hospital.common.PageResponse;
import com.example.hospital.domain.dto.ChangePasswordRequest;
import com.example.hospital.domain.dto.LoginRequest;
import com.example.hospital.domain.dto.RegisterRequest;
import com.example.hospital.domain.entity.ExaminationOrder;
import com.example.hospital.domain.entity.User;
import com.example.hospital.service.UserService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/users")
public class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }
    /**
     * 用户注册
     * @param req
     * @return
     */
    @PostMapping("/register")
    public ApiResponse<User> register(@Valid @RequestBody RegisterRequest req) {
        return ApiResponse.success(userService.register(req));
    }
    /**
     * 用户登录
     * @param req
     * @return
     */
    @PostMapping("/login")
    public ApiResponse<User> login(@Valid @RequestBody LoginRequest req) {
        return ApiResponse.success(userService.login(req.getUsername(), req.getPassword(), req.getPhone()));
    }
    /**
     * 用户修改密码
     * @param req
     * @return
     */
    @PostMapping("/change-password")
    public ApiResponse<Boolean> changePassword(@Valid @RequestBody ChangePasswordRequest req) {
        return ApiResponse.success(userService.changePassword(req));
    }
    /**
     * 获取用户个人信息
     * @param userId
     * @return
     */
    @GetMapping("/{userId}")
    public ApiResponse<User> profile(@PathVariable("userId") Long userId) {
        return ApiResponse.success(userService.getProfile(userId));
    }
    /**
     * 更新用户个人信息
     * @param userId
     * @param user
     * @return
     */
    @PutMapping("/{userId}")
    public ApiResponse<Boolean> updateProfile(@PathVariable("userId") Long userId, @RequestBody User user) {
        user.setUserId(userId);
        return ApiResponse.success(userService.updateProfile(user));
    }
    /**
     * 用户预约订单（普通项目或套餐）
     * @param userId
     * @param order
     * @return
     */
    @PostMapping("/{userId}/orders")
    public ApiResponse<ExaminationOrder> createOrder(@PathVariable("userId") Long userId, @RequestBody ExaminationOrder order) {
        order.setUserId(userId);
        return ApiResponse.success(userService.createOrder(order));
    }
    /**
     * 查看我的订单
     * @param userId
     * @param status
     * @param orderType
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/{userId}/orders")
    public ApiResponse<PageResponse<ExaminationOrder>> myOrders(@PathVariable("userId") Long userId,
                                                                @RequestParam(name = "status", required = false) Integer status,
                                                                @RequestParam(name = "orderType", required = false) String orderType,
                                                                @RequestParam(name = "page", defaultValue = "1") int page,
                                                                @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(userService.listOrders(userId, status, orderType, page, size));
    }
    /**
     * 查看我的指标结果
     * @param userId
     * @param orderId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/{userId}/indicator-results")
    public ApiResponse<PageResponse<com.example.hospital.domain.entity.IndicatorResult>> myResults(@PathVariable("userId") Long userId,
                                                                                                   @RequestParam(name = "orderId", required = false) Long orderId,
                                                                                                   @RequestParam(name = "page", defaultValue = "1") int page,
                                                                                                   @RequestParam(name = "size", defaultValue = "10") int size) {
        return ApiResponse.success(userService.listMyIndicatorResults(userId, orderId, page, size));
    }
}